home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 138_01 / bu.c < prev    next >
Text File  |  1985-08-19  |  32KB  |  1,028 lines

  1. /* 
  2. HEADER:     CUG
  3. TITLE:        BU.C - Archival File Backup Utility for CP/M
  4. VERSION:    1.1
  5. DATE:        01/18/85
  6. DESCRIPTION:    "Published under the title "Archiving Files with
  7.         CP/M-80 and CP/M-86" in the January 1985 issue of
  8.         DR. DOBB'S JOURNAL, BU is an archival file backup
  9.         utility for hard disks as well as floppies.
  10.         Functions include file copy and verification
  11.         using all of free RAM memory and full runtime
  12.         validation of CP/M file references entered on
  13.         command line."
  14. KEYWORDS:    archival, backup, CP/M, CP/M-80, CP/M-86, DDJ
  15. SYSTEM:        Any CP/M-80 or CP/M-86 system
  16. FILENAME:    BU.C
  17. WARNINGS:    "BU cannot detect files that have been updated
  18.         through random access disk writes or have had
  19.         data appended through sequential disk writes. It
  20.         will detect files that have been rewritten in
  21.         their entirety or have been renamed. In practise,
  22.         this has been found to be a minor inconvenience."
  23. CRC:        xxxx
  24. SEE-ALSO:    BU.DOC
  25. AUTHORS:    Ian Ashdown - byHeart Software
  26. COMPILERS:    Any C compiler for CP/M-80 or CP/M-86
  27. REFERENCES:    AUTHORS: Ian Ashdown;
  28.         TITLE:     'Archiving Files with CP/M-80 and
  29.              CP/M-86'
  30.              Dr. Dobb's Journal
  31.              January, 1985;
  32. ENDREF
  33. */
  34.  
  35. /*-------------------------------------------------------------*/
  36.  
  37. /* BU.C - A File Backup Utility for CP/M-80 & CP/M-86
  38.  *
  39.  * Copyright:    Ian Ashdown
  40.  *        byHeart Software
  41.  *        2 - 2016 West First Avenue
  42.  *        Vancouver, B.C. V6J 1G8
  43.  *        Canada
  44.  *
  45.  * This program may be copied for personal, non-commercial use
  46.  * only, provided that the above copyright notice is included in
  47.  * all copies of the source code. Copying for any other use
  48.  * without previously obtaining the written permission of the
  49.  * author is prohibited.
  50.  *
  51.  * pHILANTHROPICAL nOTES:
  52.  *
  53.  * Considerable time and effort went into the development of this
  54.  * software, which was expressly written for the public domain.
  55.  * The author will gladly accept any and all monetary
  56.  * contributions for the purpose of continuing such work!
  57.  *
  58.  * Acknowledgment: DeSmet C code and suggestions for program
  59.  *            improvement courtesy of Dr. Dobb's Journal
  60.  *           Contributing Editor Anthony Skjellum. This
  61.  *           program appeared in the January 1985 issue
  62.  *           of Dr. Dobb's Journal under the title of
  63.  *           "Archiving Files with CP/M-80 and CP/M-86".
  64.  *            
  65.  * Version:    1.1    Written for Aztec CII v1.06b (CP/M-80)
  66.  *            and DeSmet C88 v2.2 (CP/M-86)
  67.  *
  68.  * Date:    December 31st, 1983 (Version 1.0)
  69.  *        September 7th, 1984 (Version 1.1)
  70.  *        January 18th, 1985  (Public Domain Release)
  71.  *
  72.  * Version Modifications:
  73.  *
  74.  * 24/09/84 - "read()" and "write()" accept maximum of 32767
  75.  *          bytes, not 32768. Functions "copy_file()" and
  76.  *          "verify_file()" modified accordingly.
  77.  *
  78.  * BU utilizes the undocumented "archive" file attribute feature
  79.  * of CP/M-80 Versions 2.x and CP/M-86 to automatically detect
  80.  * files that have been changed since the disk was last "backed
  81.  * up" and copy them (with verification) to a backup disk. This
  82.  * program performs the same action as the "A" option of PIP 
  83.  * under Digital Research's MP/M 2, for which the Archive
  84.  * attribute is documented.
  85.  *
  86.  * Usage: BU x[:afn] y [-AFHQSn]
  87.  *
  88.  *      where x = drive name of disk to be backed up
  89.  *        y = drive name of backup disk
  90.  *
  91.  *      and the optional arguments are:
  92.  *
  93.  *        -A      All files, regardless of status
  94.  *        -F      Fast copy (without verification)
  95.  *        -H      Hard disk (files may be split)
  96.  *        -Q        Query each file before backup
  97.  *        -S      System attribute copied to backup
  98.  *        -n      Backup USER 'n' files only (0-31)
  99.  *        afn      Any legal CP/M ambiguous fileref
  100.  *              (can only be used with -n option)
  101.  */
  102.  
  103. #include "stdio.h"
  104. #include "ctype.h"    /* Contains macro for "isdigit()" */
  105.  
  106. /*** DEFINITIONS ***/
  107.  
  108. #define AZTEC    1    /* Aztec CII v1.06b (CP/M-80) */
  109. #define DESMET    0    /* DeSmet C88 v2.2 (CP/M-86) */
  110.  
  111. #if DESMET
  112. #define movmem(src,dest,len)    _mov(len,src,dest)
  113. #endif
  114.  
  115. #define ERROR       -1
  116. #define DEL       -1    /* Deleted fileref flag */
  117. #define ALL       -1    /* All user numbers flag */
  118. #define TRUE       -1
  119. #define FALSE        0
  120. #define SUCCESS     0
  121. #define O_RDONLY    0
  122. #define USER_ERR    0    /* Specified fileref must only be used
  123.                with -number command-line option */
  124. #define BAD_FREF    1    /* Illegal file reference */
  125. #define BAD_ARGS    2    /* Illegal command line */
  126. #define BAD_OPT     3    /* Illegal option */
  127. #define BAD_USER    4    /* Illegal user number */
  128. #define BAD_DRV     5    /* Illegal drive names */
  129. #define SAME_DRV    6    /* Same drive name for output as input */
  130. #define OPN_ERR     8    /* File open error */
  131. #define READ_ERR    9    /* File read error */
  132. #define CLS_ERR    10    /* File close error */
  133. #define BAD_VFY    11    /* File verify error */
  134. #define DIR_IO        6    /* BDOS Direct I/O service */
  135. #define RESET_DRV  13    /* BDOS Reset All Drives service */
  136. #define SEL_DRV       14    /* BDOS Select Drive service */
  137. #define SRCH_F       17    /* BDOS Search Next service */
  138. #define SRCH_N       18    /* BDOS Search Next service */
  139. #define GET_DRV       25    /* BDOS Get Default Drive service */
  140. #define SET_DMA       26    /* BDOS Set DMA Address service */
  141. #define SET_ATT       30    /* BDOS Set File Attributes service */
  142. #define USER_CODE  32    /* BDOS Get/Set User Code service */
  143. #define MAX_USER   32    /* 32 user codes under CP/M (see DR's
  144.                documentation for BDOS Service 32) */
  145.  
  146. /*** GLOBAL VARIABLES ***/
  147.  
  148. char ent_drv,    /* Entry drive code */
  149.      ent_user,    /* Entry user code */
  150.      cur_user;    /* Current user code */
  151.  
  152. /*** MAIN BODY OF CODE ***/
  153.  
  154. main(argc,argv)
  155. int argc;
  156. char *argv[];
  157. {
  158.   char in_dsk,        /* Drive name of input disk */
  159.        out_dsk,        /* Drive name of output (backup) disk */
  160.        in_drv,        /* Drive code of input disk */
  161.        out_drv,        /* Drive code of output disk */
  162.        seg_no,        /* Segment number for split files */
  163.        in_file[15],    /* Fileref of current input file */
  164.        out_file[15],    /* Fileref of current output file */
  165.        c,        /* Scratch variable */
  166.        *s,        /* Scratch string pointer */
  167.        *buffer,        /* Pointer to directory entry returned */
  168.             /* by "srch_file()" */
  169.        *srch_file(),
  170.        *malloc();
  171.  
  172.   /* File control blocks of current input and output files */
  173.  
  174.   static char in_fcb[33],    /* (Automatically initialized */
  175.           out_fcb[33];    /* to zero by compiler) */
  176.  
  177.   /* Structure for linked list of filerefs */
  178.  
  179.   struct file_ref
  180.   {
  181.     char name[12];        /* File reference */
  182.     struct file_ref *next;    /* Pointer to next instance */
  183.   } root,            /* Start of linked list */
  184.     *fref_1,            /* Scratch pointers to */
  185.     *fref_2;            /* linked list instances */
  186.  
  187.   /* Initialized file control block for "srch_file()". This FCB
  188.      is for a fully ambiguous fileref that causes "srch_file()"
  189.      to return all directory entries for the current default
  190.      drive. */
  191.  
  192.   static char fcb[] = {'?','?','?','?','?','?','?','?',
  193.                '?','?','?','?','?',0,0,0};
  194.  
  195.   int file_cnt = 0,    /* Count of file to be backed up */
  196.       dup_flag,        /* Duplicate fileref flag */
  197.       all_files,    /* All_files flag (cmd-line option) */
  198.       fast_copy,    /* Fast copy flag (cmd-line option) */
  199.       hard_disk,    /* Hard disk flag (cmd-line option) */
  200.       query,        /* Query flag (cmd-line option) */
  201.       system,        /* System flag (cmd-line option) */
  202.       user_no,        /* User number (cmd-line option) */
  203.       next_flag = FALSE;/* Flag to indicate to "srch_file()"
  204.                that a "search next" is required */
  205.  
  206.   register int i,j;    /* Loop indices */
  207.  
  208.   long begin,        /* Input file position variables */
  209.        end;
  210.  
  211.   /* Display program header */
  212.  
  213.   printf("\nBU Version 1.1");
  214.   printf("                           Copyright 1983, 1984");
  215.   printf(" byHeart Software\n\n");
  216.  
  217.   /* Initialize command-line options */
  218.  
  219.   all_files = FALSE;    /* Copy only non-archived files */
  220.   fast_copy = FALSE;    /* Copy files with verification */
  221.   hard_disk = FALSE;    /* Files will not be split across backup
  222.                disks if remaining capacity of backup
  223.                disk is less than current file size */
  224.   query = FALSE;    /* Backup without query */
  225.   system = FALSE;    /* Assign directory attribu